<?php

namespace app\api\logic;

use app\api\model\User;
use app\api\model\UserCode;
use app\api\model\UserToken;
use app\api\util\ImageUtil;
use think\facade\Db;
use think\facade\Log;

class UserLogic
{
    // 注销账号
    public static function logoff($userId) {
        return User::logoff($userId);
    }

    // 验证码登录
    public static function loginByCode($phone, $code) {
        // 验证code
        $codeLine = UserCode::check($phone, $code);
        if(!$codeLine) {
            \Debug::setDebug('验证码错误');
            return false;
        }

        // 修改验证码为已使用
        if(isProduction()) {
            UserCode::changeUse($codeLine->getId());
        }

        return self::loginByPhone($phone);
    }

    // 手机号登录-验证码/一键登录
    public static function loginByPhone($phone) {
        // 根据手机号查询用户信息
        try {
            Db::startTrans();

            $user = User::getByPhone($phone);
            if ($user) {
                // 判断是否被封号
                if ($user->isLogoff()) {
                    Db::rollback();
                    \Debug::setDebug('该账号已被注销.请联系客服');
                    return false;
                }
                $userId = $user->getId();
            }
            if (!$user) {
                // 如果有邀请码,找到父ID

                // 没有用户信息,注册用户

                $userId = User::registerByPhone($phone);
                if (!$userId) {
                    Db::rollback();
                    \Debug::setDebug('注册失败');
                    return false;
                }
            }

            Db::commit();
        } catch (\Throwable $e) {
            Db::rollback();
            $msg = sprintf("%s in %s:%s", $e->getMessage(), $e->getFile(), $e->getLine());
            Log::error($msg);
            return false;
        }

        // 处理用户token 并返回token
        $token = UserToken::flush($userId, $phone);

        return self::getUserApi($userId, $token);
    }

    public static function getInfoByUserId($userId) {
        return self::getUserApi($userId);
    }

    public static function getUserApi($userId, $token='') {
        // 查询基本信息
        $user = User::getInfo($userId);
        if(!$user) {
            \Debug::setDebug('获取用户信息失败');
            return false;
        }

        if(!$token) {
            $token = UserToken::getTokenByUserId($userId);
        }

        $nickName = $user->getNickName();
        $userImage = ImageUtil::resize($user->getImage(), 400);
        $phone = $user->getPhone();

        $data = self::_fillUserDataRes($token, $userId, $nickName, $userImage, $phone);
        return $data;
    }

    private static function _fillUserDataRes($token, $userId=0, $nickName='', $userImage='', $phone='') {
        $data = [
            'id'    => $userId,
            'nickname' => $nickName,
            'image' => $userImage,
            'phone' => $phone,
            'token'     => $token,
        ];
        return $data;
    }
}